package org.yelongframework.pdm.xml;

import java.util.Date;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.yelongframework.commons.lang.LongUtil;
import org.yelongframework.pdm.PdmColumn;
import org.yelongframework.util.Assert;

/**
 * XML实现
 * 
 * @since 1.0.0
 */
/**
 * 
 * @since 1.0.0
 */
public class DefaultXmlPdmColumn implements PdmColumn {

	private static final long serialVersionUID = 3137825685086521702L;

	private final OTable oTable;

	private final OColumn oColumn;

	public DefaultXmlPdmColumn(OTable oTable, OColumn oColumn) {
		Assert.notNull(oTable, "oTable cannot be null");
		Assert.notNull(oColumn, "oColumn cannot be null");
		this.oTable = oTable;
		this.oColumn = oColumn;
	}

	@Override
	public String getId() {
		return oColumn.getId();
	}

	@Override
	public void setId(String id) {
		oColumn.setId(id);
	}

	@Override
	public String getObjectId() {
		return oColumn.getaObjectId();
	}

	@Override
	public void setObjectId(String objectId) {
		oColumn.setaObjectId(objectId);
	}

	@Override
	public String getCreator() {
		return oColumn.getaCreator();
	}

	@Override
	public void setCreator(String creator) {
		oColumn.setaCreator(creator);
	}

	@Override
	public String getCreationDateStr() {
		return oColumn.getaCreationDate();
	}

	@Override
	public void setCreationDateStr(String creationDateStr) {
		oColumn.setaCreationDate(creationDateStr);
	}

	@Override
	public Date getCreationDate() {
		String creationDateStr = getCreationDateStr();
		if (StringUtils.isBlank(creationDateStr)) {
			return null;
		}
		return new Date(Long.valueOf(creationDateStr));
	}

	@Override
	public void setCreationDate(Date creationDate) {
		setCreationDateStr(null == creationDate ? null : creationDate.getTime() + "");
	}

	@Override
	public String getModifier() {
		return oColumn.getaModifier();
	}

	@Override
	public void setModifier(String modifier) {
		oColumn.setaModifier(modifier);
	}

	@Override
	public String getModificationDateStr() {
		return oColumn.getaModificationDate();
	}

	@Override
	public void setModificationDateStr(String modificationDateStr) {
		oColumn.setaModificationDate(modificationDateStr);
	}

	@Override
	public Date getModificationDate() {
		String modificationDateStr = getModificationDateStr();
		if (StringUtils.isBlank(modificationDateStr)) {
			return null;
		}
		return new Date(Long.valueOf(modificationDateStr));
	}

	@Override
	public void setModificationDate(Date modificationDate) {
		setModificationDateStr(null == modificationDate ? null : modificationDate.getTime() + "");
	}

	@Override
	public String getName() {
		return oColumn.getaName();
	}

	@Override
	public void setName(String name) {
		oColumn.setaName(name);
	}

	@Override
	public String getCode() {
		return oColumn.getaCode();
	}

	@Override
	public void setCode(String code) {
		oColumn.setaCode(code);
	}

	@Override
	public String getComment() {
		return oColumn.getaComment();
	}

	@Override
	public void setComment(String comment) {
		oColumn.setaComment(comment);
	}

	@Override
	public String getDataType() {
		return oColumn.getaDataType();
	}

	@Override
	public void setDataType(String dataType) {
		oColumn.setaDataType(dataType);
	}

	@Override
	public String getLengthStr() {
		return oColumn.getaLength();
	}

	@Override
	public void setLengthStr(String lengthStr) {
		oColumn.setaLength(lengthStr);
	}

	@Override
	public Long getLength() {
		return LongUtil.valueOf(getLengthStr());
	}

	@Override
	public void setLength(Long length) {
		setLengthStr(null == length ? null : length.toString());
	}

	@Override
	public String getMandatory() {
		return oColumn.getaColumnMandatory();
	}

	@Override
	public void setMandatory(String mandatory) {
		oColumn.setaColumnMandatory(mandatory);
	}

	@Override
	public boolean getMandatoryBoolean() {
		if (StringUtils.isBlank(getMandatory())) {
			return false;
		}
		return "1".equals(getMandatory());
	}

	@Override
	public void setMandatoryBoolean(boolean mandatory) {
		setMandatory(mandatory ? "1" : null);
	}

	@Override
	public boolean isPrimaryKey() {
		CPrimaryKey cPrimaryKey = oTable.getcPrimaryKey();
		if (null == cPrimaryKey) {
			return false;
		}
		List<String> oKeyRefList = cPrimaryKey.getoKeyRefList();
		if (CollectionUtils.isEmpty(oKeyRefList)) {
			return false;
		}
		CKeys cKeys = oTable.getcKeys();
		if (null == cKeys) {
			return false;
		}
		List<OKey> oKeyList = cKeys.getoKeyList();
		if (CollectionUtils.isEmpty(oKeyList)) {
			return false;
		}
		for (OKey oKey : oKeyList) {
			if (!oKeyRefList.contains(oKey.getId())) {
				continue;
			}
			CKeyColumns cKeyColumns = oKey.getcKeyColumns();
			if (null == cKeyColumns) {
				continue;
			}
			List<String> oColumnRefList = cKeyColumns.getoColumnRefList();
			if (CollectionUtils.isEmpty(oColumnRefList)) {
				continue;
			}
			if (oColumnRefList.contains(getId())) {
				return true;
			}
		}
		return false;
	}

	@Override
	public String toString() {
		return oColumn.toString();
	}

}
